package com.jeff.android.lab.module.anim.path;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * @author: duff
 * @date: 2019/1/3
 * @since: 1.0.0
 */
public class BottomArcBackground extends View {
    private Paint mPaint;
    private Path mPath;

    private int mWidth = 0;
    private int mHeight = 0;

    private float mRatio = .9f;

    public BottomArcBackground(Context context) {
        super(context);
        init();
    }

    public BottomArcBackground(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BottomArcBackground(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(1);
        mPaint.setAntiAlias(true);

        mPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;

        LinearGradient lg = new LinearGradient(0, 0, mWidth, 0,
                Color.parseColor("#00AFF9"), Color.parseColor("#057AFF"),
                Shader.TileMode.CLAMP);
        mPaint.setShader(lg);

        if (mHeight < 750) {
            mRatio = .8f;
        } else if (mHeight < 1050) {
            mRatio = .85f;
        } else if (mHeight < 1500) {
            mRatio = .9f;
        } else {
            mRatio = .95f;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();

        mPath.moveTo(0, (int) (mRatio * mHeight));
        mPath.quadTo(.5f * mWidth, mHeight + (int) ((1 - mRatio) * mHeight),
                mWidth, (int) (mRatio * mHeight));

        mPath.lineTo(mWidth, 0);
        mPath.lineTo(0, 0);
        mPath.close();

        canvas.drawPath(mPath, mPaint);
    }

}
